local jituan = fk.CreateSkill{
  name = "rfenghou__jituan",
}

--- 随机应变！
---@param logic GameLogic
---@param player ServerPlayer
---@return string? @ 牌名
local function getFinalUse(logic, player)
	local events = logic.event_recorder[GameEvent.UseCard]
  if not events or #events == 0 then return nil end
  for i = #events, 1, -1 do
    local e = events[i]
    if e.data.from == player then
      local card = e.data.card
      if card.type == Card.TypeBasic or card:isCommonTrick() then
        return card.name
      end
    end
  end
  return nil
end

jituan:addEffect("active", {
  name = "rfenghou__jituan",
  anim_type = "offensive",
  prompt = "#rfenghou__jituan-prompt",
  can_use = function (self, player)
    return player:usedSkillTimes(jituan.name, Player.HistoryPhase) == 0
    and not player:isKongcheng()
  end,
  card_num = 0,
  target_num = 1,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected)
    if #selected > 0 then return false end
    return player:canPindian(to_select)
  end,
  on_use = function (self, room, effect)
    local player = effect.from
    local target = effect.tos[1]

    local drawAndUse = function ()
      local name = getFinalUse(room.logic, player)
      if room:askToSkillInvoke(player, {
        skill_name = jituan.name,
        prompt = "#rfenghou__jituan-draw"
      }) then
        player:drawCards(1, jituan.name)

        if not name then return end
        local card = Fk:cloneCard(name)
        card.skillName = jituan.name
        if player:isAlive() and not player:isKongcheng() and card:getAvailableTargets(player, { bypass_times = true }) then
          room:askToUseVirtualCard(player, {
            name = name,
            card_filter = {
              n = 1,
              cards = player:getHandlyIds()
            },
            prompt = "#rfenghou__jituan-viewas:::"..name,
            skill_name = jituan.name
          })
        end
      end
    end

    local pindian = player:pindian({target}, jituan.name)
    if pindian.results[target].winner ~= player then
      drawAndUse()
    end

    if not player:isAlive() then return end
    local now = room.logic:getCurrentEvent()
    local use = room:askToUseVirtualCard(player, {
      name = "duel",
      skill_name = jituan.name
    })
    if use and #room.logic:getEventsByRule(GameEvent.Damage, 1, function (d_event)
      local data = d_event.data
      local c_event = d_event:findParent(GameEvent.CardEffect, false, 2)
      if c_event == nil then return false end
      local c_effect = c_event.data
      return data.card and data.card == c_effect.card and data.card == use.card and data.to == player
    end, now.id) == 1 then
      drawAndUse()
    end

    if not player:isAlive() or player:isKongcheng() then return end
    local tos = table.filter(room:getOtherPlayers(player, false), function (p)
      return player:canPindian(p)
    end)
    if #tos > 0 then
      local to = room:askToChoosePlayers(player, {
        targets = tos,
        min_num = 1,
        max_num = 1,
        prompt = "#rfenghou__jituan-pindian",
        skill_name = jituan.name,
        cancelable = false
      })
      pindian = player:pindian(to, jituan.name)
      if pindian.results[to[1]].winner ~= player then
        drawAndUse()
      end
    end
  end
})

Fk:loadTranslationTable{
  ["rfenghou__jituan"] = "击湍",
  [":rfenghou__jituan"] = "出牌阶段限一次，你可以依次：拼点、视为使用【决斗】、拼点，过程中每次没赢后，你可以摸一张牌，然后将一张手牌当<a href=':adaptation'>【随机应变】</a>使用。",

  ["#rfenghou__jituan-prompt"] = "击湍：你可以拼点、视为使用【决斗】、拼点<br/>过程中每次没赢后，你可以摸一张牌，将一张手牌当【随机应变】使用",
  ["#rfenghou__jituan-draw"] = "击湍：可以摸一张牌，将一张手牌当【随机应变】使用",
  ["#rfenghou__jituan-viewas"] = "击湍：将一张手牌当【%arg】使用",
  ["#rfenghou__jituan-pindian"] = "击湍：与一名角色拼点",
}

return jituan
